<?php 
	
/**
* logging.php	
* Clase utilizada para logging en base de datos
* 
*
*/	
class Logging {
	public $oDB;
    	
  	function __construct() {
   		require_once('/home/freshbuzzing/www/140dev/db/db_lib.php');
    	$this->oDB = new db;  	
  	}
  	
  	public function init_log($desc_proceso){
  		$this->oDB->insert('LOGS_PROCESS',"DT_PROCESS_INI=NOW(), DT_PROCESS_END=NULL, DS_PROCESS_NAME='" . $desc_proceso . "', DS_RESULT_PROCESS=NULL, BL_OK=NULL");
  		$result = $this->oDB->select('SELECT MAX(ID_PROCESS) FROM LOGS_PROCESS');
  		if($row = mysqli_fetch_row($result))
  		{
  			return $row[0];
  		}
  		else
  			return -1;
  	}
  	
  	public function end_log($num_proceso, $ds_result, $bl_ok){
  		$this->oDB->update('LOGS_PROCESS', "DT_PROCESS_END=NOW(), DS_RESULT_PROCESS = '" . $ds_result . "', BL_OK= '" . $bl_ok . "'", "ID_PROCESS = " . $num_proceso);
  	}
  	
  	public function log_details($num_proceso,$process_master_name,$process_name,$ds_event_text){
  		$this->oDB->insert('LOGS_DETAILS_PROCESS',"ID_PROCESS=" . $num_proceso . ", DT_EVENT=NOW(), DS_MASTER_PROCESS_NAME='" . $process_master_name . "', DS_PROCESS_NAME='" . $process_name . "', DS_EVENT_TEXT='" . $ds_event_text . "'");
  	}
  	
  	public function enviar_correo($num_proceso,$nom_proceso){
  		$sql = "SELECT T1.DS_PROCESS_NAME, DS_RESULT_PROCESS, DS_EVENT_TEXT ";
  		$sql = $sql . "FROM LOGS_PROCESS T1 INNER JOIN LOGS_DETAILS_PROCESS T2 ON ";
  		$sql = $sql . "T1.ID_PROCESS = T2.ID_PROCESS ";
  		$sql = $sql . "WHERE T1.ID_PROCESS='" . $num_proceso . "'";
  		 
  		$result = $this->oDB->select($sql);
  		$strErrores = "";
  		$cont = 0;
		while($row = mysqli_fetch_row($result))
		{
			$strErrores = $strErrores . $row[2] . "<br/>";
			$cont = $cont + 1;
		}			
		
		//vars
		$subject = "Procesos - " . $nom_proceso . " - ";
		if($cont>0)
			$subject = $subject . " NOK - " . $cont . " errores!!!";
		else
			$subject = $subject . " OK";
			
		$to = explode(',', "info@sygnium.com" );

		$from = "info@sygnium.com";

		//Headers
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=UTF-8\r\n";
		$headers .= "From: <info@sygnium.com>" ;
 
		//send for each mail
		foreach($to as $mail){
		   mail($mail, $subject, $strErrores, $headers);
		}
  	}
}
	  	
	
?>